package com.globalresources.ui.struts.action.user;

import java.util.List;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.servlet.http.HttpServletRequest;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;
import com.globalresources.business.inface.IUserService;
import com.globalresources.business.inface.ICityService;
import com.globalresources.business.inface.IHospitalService;
import com.globalresources.business.inface.IDetailUserService;
import com.globalresources.commons.AbstractAction;
import com.globalresources.commons.Pager;
import com.globalresources.commons.PagerService;
import com.globalresources.po.User;
import com.globalresources.po.City;
import com.globalresources.po.Hospital;

import com.globalresources.po.DetailUser;
/**
 * @author songduanlei@gmail.com
 * @since 创建时间：Jul 16, 2009 1:02:45 AM
 */

public class UserAction extends AbstractAction implements ServletRequestAware{
	
	private HttpServletRequest request;
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}	
	
	private List<User> users;
	private Pager pager;	
	private PagerService pagerService;
	private IUserService userService;
	private String currentPage;
	private String pagerMethod;
	private String totalRow;	
	private String pusername;		
	private User user;
	private String message;
	
	private String oldPassword;
	private String newPassword;
	private String newPassword2;	
	private String username;

	private IHospitalService hospitalService;
	private ICityService cityService;
	private List<City> citys;
	private List<Hospital> hospitals;
	
	private DetailUser detailUser;
	private IDetailUserService detailUserService;
	
	/***
	 * @author songduanlei@gmail.com
	 * 返回所有用户信息
	 * @return
	 */
	public String list(){
		int _totalRow = userService.countUsers(pusername);
		pager = pagerService.getPager(currentPage, pagerMethod, _totalRow);
		setCurrentPage(String.valueOf(pager.getCurrentPage()));
		setTotalRow(String.valueOf(_totalRow));
		users = userService.getUsers(pusername);
		return SUCCESS;
	}
	
	/***
	 * @author songduanlei@gmail.com
	 * 查看个人信息
	 * @return
	 */
	public String getUserInfo(){
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (null != obj && obj instanceof UserDetails){
			UserDetails ud = (UserDetails)obj;
			List<User> list = userService.getUser(ud.getUsername());
			if (null != list && list.size() > 0) {
				user = (User)list.get(0);	
			
			}
		}		
		return SUCCESS;
	}
	/***
	 * @author songduanlei@gmail.com
	 * 修改个人信息
	 * @return
	 */
	public String editUserInfo(){
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (null != obj && obj instanceof UserDetails){
			UserDetails ud = (UserDetails)obj;
			List<User> list = userService.getUser(ud.getUsername());
			if (null != list && list.size() > 0) {
				user = (User)list.get(0);	
				citys=cityService.listCity();
				hospitals=hospitalService.listHopistal();
			}
		}		
		return SUCCESS;
	}
	/***
	 * @author songduanlei@gmail.com
	 * 修改 用户信息
	 * @return
	 */
	public String editUser(){
		    String id=request.getParameter("id");
		    user= userService.getUserById(id);
			citys=cityService.listCity();
			hospitals=hospitalService.listHopistal();
			return SUCCESS;
	}
	
	
	/***
	 * @author songduanlei@gmail.com
	 * 添加 用户信息
	 * @return
	 */
	public String addUser(){
		    user=null;
			citys=cityService.listCity();
			hospitals=hospitalService.listHopistal();
			return SUCCESS;
	}
	
	
	/***
	 * @author songduanlei@gmail.com
	 * 保存个人信息
	 * @return
	 */
	public String saveUserInfo(){

			if (null != user) {
				detailUser=user.getDetailUser();
				detailUserService.saveDetailUser(detailUser);
				userService.saveUser(user);	
			}
			return SUCCESS;
		}
	
	/***
	 * @author songduanlei@gmail.com
	 * 保存个人信息
	 * @return
	 */
	public String saveOrUpdateUserInfo(){
			if (null != user) {
				detailUser=user.getDetailUser();
				detailUserService.saveOrUpdateDetailUser(detailUser);
				userService.saveOrUpdateUser(user);
				setMessage("Update Success!");
			}
			return SUCCESS;
		}
	
	
	/***
	 * @author songduanlei@gmail.com
	 * Change User Password
	 * @return
	 */
	public String editPassword() {

		Object obj = SecurityContextHolder.getContext().getAuthentication()
				.getPrincipal();
		if (null != obj && obj instanceof UserDetails) {
			UserDetails ud = (UserDetails) obj;
			List<User> list = userService.getUser(ud.getUsername());
			if (null != list && list.size() > 0) {
				user = (User) list.get(0);
				String password = user.getPassword();
				if (!password.equals(oldPassword)) {
					setMessage("oldPassword is not ture");
					return ERROR;
				} else {
					if (!newPassword.equals(newPassword2)) {
						setMessage("newPassword is not equal to the confirm new Password");
						return ERROR;
					} else {

						user.setPassword(newPassword);
						userService.saveOrUpdateUser(user);
						setMessage("change password success");
						return SUCCESS;

					  }
				    }
			}else{
				return ERROR;
			}
		}else{
			return ERROR;
		}

	}
	
	public String index(){
		Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		if (obj instanceof UserDetails) {
			UserDetails userDetails = (UserDetails)obj;
			setUsername(userDetails.getUsername());
		}		
		return SUCCESS;
	}
	
/*	public String getUser(){
		int _totalRow = userService.countUsers(pusername);
		pager = pagerService.getPager(currentPage, pagerMethod, _totalRow);
		setCurrentPage(String.valueOf(pager.getCurrentPage()));
		setTotalRow(String.valueOf(_totalRow));
		users = userService.getUsers(pusername);
		return SUCCESS;
	}
	*/
	
	
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public PagerService getPagerService() {
		return pagerService;
	}
	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	public String getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}
	public String getPagerMethod() {
		return pagerMethod;
	}
	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}
	public String getTotalRow() {
		return totalRow;
	}
	public void setTotalRow(String totalRow) {
		this.totalRow = totalRow;
	}

	public String getPusername() {
		return pusername;
	}

	public void setPusername(String pusername) {
		this.pusername = pusername;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	/**
	 * @return the oldPassword
	 */
	public String getOldPassword() {
		return oldPassword;
	}
	/**
	 * @param oldPassword the oldPassword to set
	 */
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	/**
	 * @return the newPassword
	 */
	public String getNewPassword() {
		return newPassword;
	}
	/**
	 * @param newPassword the newPassword to set
	 */
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	/**
	 * @return the newPassword2
	 */
	public String getNewPassword2() {
		return newPassword2;
	}
	/**
	 * @param newPassword2 the newPassword2 to set
	 */
	public void setNewPassword2(String newPassword2) {
		this.newPassword2 = newPassword2;
	}
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}
	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @return the hospitalService
	 */
	public IHospitalService getHospitalService() {
		return hospitalService;
	}

	/**
	 * @param hospitalService the hospitalService to set
	 */
	public void setHospitalService(IHospitalService hospitalService) {
		this.hospitalService = hospitalService;
	}

	/**
	 * @return the cityService
	 */
	public ICityService getCityService() {
		return cityService;
	}

	/**
	 * @param cityService the cityService to set
	 */
	public void setCityService(ICityService cityService) {
		this.cityService = cityService;
	}

	/**
	 * @return the citys
	 */
	public List<City> getCitys() {
		return citys;
	}

	/**
	 * @param citys the citys to set
	 */
	public void setCitys(List<City> citys) {
		this.citys = citys;
	}

	/**
	 * @return the hospitals
	 */
	public List<Hospital> getHospitals() {
		return hospitals;
	}

	/**
	 * @param hospitals the hospitals to set
	 */
	public void setHospitals(List<Hospital> hospitals) {
		this.hospitals = hospitals;
	}

	/**
	 * @return the detailUser
	 */
	public DetailUser getDetailUser() {
		return detailUser;
	}

	/**
	 * @param detailUser the detailUser to set
	 */
	public void setDetailUser(DetailUser detailUser) {
		this.detailUser = detailUser;
	}

	/**
	 * @return the detailUserService
	 */
	public IDetailUserService getDetailUserService() {
		return detailUserService;
	}

	/**
	 * @param detailUserService the detailUserService to set
	 */
	public void setDetailUserService(IDetailUserService detailUserService) {
		this.detailUserService = detailUserService;
	}
}
